package teacher.action.student;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import teacher.bean.Studegreepaper;
import teacher.bean.Teacher;
import teacher.bean.Treatise;
import teacher.service.student;
import teacher.service.treatise;
import teacher.bean.Students;
import tools.StringDate;

public class uploadDeg extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private Studegreepaper studegreepaper;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	private int stuId2;
	String suc;

	public int getStuId2() {
		return stuId2;
	}

	public void setStuId2(int stuId2) {
		this.stuId2 = stuId2;
	}

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}
	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}

	public Studegreepaper getStudegreepaper() {
		return studegreepaper;
	}

	public void setStudegreepaper(Studegreepaper studegreepaper) {
		this.studegreepaper = studegreepaper;
	}

	public String execute() throws Exception {

		if(fileCom!=null)
		{
			
			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");

			File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
	            FilePath=uploadPath+File.separator+this.getFileComFileName();

	     	    File toFile = new File(uploadPath, this.getFileComFileName());

	     		OutputStream os = new FileOutputStream(toFile);

	     		byte[] buffer = new byte[4096];

	     		int length = 0;

	     		while ((length = is.read(buffer)) > 0) {
	     			os.write(buffer, 0, length);
	     		}
	     		System.out.println("上传文件名"+fileComFileName);
	     		System.out.println(FilePath);

	     		is.close();

	     		os.close();
			
	     		studegreepaper.setUpload(fileComFileName);
	     		studegreepaper.setStuId(stuId2);
		}
		if(new student().insertDeg(studegreepaper)) {
			this.setSuc("success");
			return SUCCESS;		
		} else {
			return ERROR;
		}
	}

}